var zhengque = 0;
//		   var zhengque1 = 0;
		   var shibai = 0;
		   var shijian = 0;
		   var n = 0;
		   var s = 0;
		   var p = 0;
		    window.onload=function(){
		    	var top1 = document.getElementById("top1")
		    	var main2 = document.getElementById("main2")
//		    	var top2 = document.getElementById("top2");
//		    	var bottom1 = document.getElementById("bottom1");
                var span1 = document.getElementById("span1");
		    	var span2 = document.getElementById("span2");
		    	var span3 = document.getElementById("span3");
		    	var span4 = document.getElementById("span4");
		    	var span5 = document.getElementById("span5");
		    	var lanzi = document.getElementById("lanzi");
		    	var pghh = document.getElementById("pg");
		    }
		     
		    window.onkeydown=function(){
		    	//打苹果
		    	var key = event.keyCode;
		    	for (var i = 0;i <top1.children.length;i++) {
		    		var keycode = top1.children[i].getAttribute("keyCode");
		    		if (key == keycode) {
		    			top1.removeChild(top1.children[i]);
		    			zhengque++;
		    			span2.innerHTML = zhengque + "个";
		    			//判断过关条件
				        select2 = document.getElementById("select2");
			            se2 = select2.children[select2.selectedIndex].innerHTML;
			//			console.log(se2);
			//			console.log(sum1)
			            if(zhengque == se2) {
				            alert("恭喜过关");
				            jieShu();
			            }
		    			if (zhengque >= 10) {
//		    				console.log(zhengque);
					       document.getElementById('a1').style.display = 'block'
				        } 
				        if(zhengque >= 20){
				        	document.getElementById('a2').style.display = 'block'
				        }
				        if(zhengque >= 30){
				        	document.getElementById('a3').style.display = 'block'
				        }
				        if(zhengque >= 40){
				        	document.getElementById('a4').style.display = 'block'
				        }
				        if(zhengque >= 50){
				        	document.getElementById('a5').style.display = 'block'
				        }
				        if(zhengque >= 60){
				        	document.getElementById('a6').style.display = 'block'
				        }
				        if(zhengque >= 70){
				        	document.getElementById('a7').style.display = 'block'
				        }
				        if(zhengque >= 80){
				        	document.getElementById('a8').style.display = 'block'
				        }
				        if(zhengque >= 90){
				        	document.getElementById('a9').style.display = 'block'
				        }
				        if(zhengque >= 100){
				        	document.getElementById('a10').style.display = 'block'
				        }
		    			break;
		    		}
		    	}
		    	
		    }
			function kaiShi(){
				if (n == 0 && s == 0 && p == 0) {
					//每隔多少秒创建一个苹果
//			 	    n = setInterval("chuanPG()",1000);
			 	    select1 = document.getElementById("select1");
			        var sel1 = select1.children[select1.selectedIndex].innerHTML;
			        console.log(sel1);
			        if(sel1 == '简单') {
				        n = setInterval('chuanPG()', 1000);
				         //每隔多少秒移动
				        s = setInterval("yidongPG()",500);
			        };
			        if(sel1 == '一般') {
				        n = setInterval('chuanPG()', 800);
				         //每隔多少秒移动
				        s = setInterval("yidongPG()",400);
			        };
			        if(sel1 == '适中') {
				        n = setInterval('chuanPG()', 600);
				         //每隔多少秒移动
				        s = setInterval("yidongPG()",300);
			        };
			        if(sel1 == '困难') {
				        n = setInterval('chuanPG()', 200);
				         //每隔多少秒移动
				        s = setInterval("yidongPG()",100);
			        };
			        if(sel1 == '变态') {
				        n = setInterval('chuanPG()', 100);
				         //每隔多少秒移动
				        s = setInterval("yidongPG()",50);
			        };
			        //每隔多少秒移动
//				    s = setInterval("yidongPG()",500);
				    //每隔多少秒改变值
				    p = setInterval("panDuan()",1000);
				}
			}
			//创建苹果
			function chuanPG(){
				var div = document.createElement("div");
				var num = parseInt(Math.random()*26)+65; //苹果显示A-Z
				div.innerHTML ="&#" + num + ";"; 
				div.setAttribute("keyCode",num);
				var left = parseInt(Math.random()*690)+10;
				div.style.left = left + "px";
				div.style.top = "0px";
				top1.appendChild(div);
			}
			//移动苹果
			function yidongPG(){
			    var div = top1.getElementsByTagName("div");
			    //直接找创建的div
				for (var i = 0;i < div.length;i++) {
					var t = parseInt(div[i].style.top);
					 t += 10;
					 div[i].style.top = t + "px";
					 //苹果破裂
					 if (t > 370) {
					 	div[i].innerHTML = "";
					 	div[i].className = "posuiPG"
					 	shibai++;
					 	span3.innerHTML = shibai + "个";
					 	//判断失败条件
					 	select3 = document.getElementById("select3");
			            se3 = select3.children[select3.selectedIndex].innerHTML;
			            if(shibai == se3) {
				            alert("low爆了");
				            jieShu();
			            }
					}
					if(t > 400){
					 	top1.removeChild(div[i]);
					}
				}
				
				
			    //子节点top1.children
//              for (var i = 0;i < top1.children.length;i++) {
//              	var t = parseInt(top1.children[i].style.top);
//              	t += 10;
//              	top1.children[i].style.top = t + "px";
//              	if (t > 370) {
//              		top1.children[i].innerHTML = "";
//              		top1.children[i].className = "posuiPG"
//              	}
//              	if(t > 400){
//              		top1.removeChild(top1.children[i]);
//              	}
//              }
			}
			//暂停苹果
			function zanTing(){
				clearInterval(n);
				n = 0;
				clearInterval(s);
				s = 0;
				clearInterval(p);
				p = 0;
			}
			//结束苹果
			function jieShu(){
				zanTing();
				zhengque = 0;
				shibai = 0;
				shijian = 0;
				span1.innerHTML = "0%";
				span2.innerHTML = "0个";
				span3.innerHTML = "0个";
				span4.innerHTML = "0秒";
				span5.innerHTML = "0字/分钟"
				top1.innerHTML = "";
				document.getElementById('a1').style.display = 'none'
				document.getElementById('a2').style.display = 'none'
				document.getElementById('a3').style.display = 'none'
				document.getElementById('a4').style.display = 'none'
				document.getElementById('a5').style.display = 'none'
				document.getElementById('a6').style.display = 'none'
				document.getElementById('a7').style.display = 'none'
				document.getElementById('a8').style.display = 'none'
				document.getElementById('a9').style.display = 'none'
				document.getElementById('a10').style.display = 'none'
			}
			
			//显示
			function panDuan(){
				shijian++;
				if (zhengque + shibai > 0) {
					span1.innerHTML = parseInt(zhengque/(zhengque+shibai)*100)+"%";
				}
//				span2.innerHTML = zhengque + "个";
//				span3.innerHTML = shibai + "个";
				if (shijian < 10) {
				    span4.innerHTML = "0" + shijian + "秒";
				}else{
					span4.innerHTML = shijian + "秒";
				}
				if (zhengque > 0) {
					span5.innerHTML = parseInt(zhengque / (shijian / 60))+"字/分钟";
				}
			}
			
			//设置菜单
			function shiZhi(){
				var main2 = document.getElementById("main2");
				if (main2.style.display == "block") {
					main2.style.display = "none";
				}else{
					main2.style.display = "block";
				}
			}
			//确认按钮
			function queren() {
				var main2 = document.getElementById("main2");
			    main2.style.display = "none"
		    }
			//取消按钮
			function quxiao() {
				var main2 = document.getElementById("main2");
			    main2.style.display = "none"
		    }
			//默认按钮
			function moren() {
				select1 = document.getElementById("select1");
			    select2 = document.getElementById("select2");
			    select3 = document.getElementById("select3");
				select1.children[select1.selectedIndex].innerHTML = "1";
			    select2.children[select2.selectedIndex].innerHTML = "20";
			    select3.children[select3.selectedIndex].innerHTML = "100";
		    }
			
			